// pages/detail/index.js
import Toast from '../../weapp/toast/toast';
const App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    id: '',
    curIndex: '',
    activeNames: ['1'],
    record_filed_name: [],
    is_tel:false,
    follow_form: {
      customer_id: '',
      content: ''
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    // if(options.post_index == 2){
    //   this.getPublicCustomerDetail(options.id)
    // }else{
    //   this.getCustomerDetail(options.id)
    // }
    // this.setData({
    //   isee:options.isee
    // })
    console.log(options)
    this.setData({
      ...this.data.follow_form,
      'follow_form.customer_id': options.id,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    let options = currPage.options;
    console.log(options);
    if (options.post_index == 2) {
      this.getPublicCustomerDetail(options.id)
    } else {
      this.getCustomerDetail(options.id)
    }
    this.setData({
      id: options.id,
      tabid: options.tabid,
      curIndex: options.post_index //公池详情判断标识
    })
  },


  toDetail() {
    wx.navigateTo({
      url: `../edit/index?type=edit&id=${this.data.id}`,
    })
  },
  toOperation() {
    wx.navigateTo({
      url: `../handle-info/index?type=edit&id=${this.data.id}&curIndex=${this.data.curIndex}&tabid=${this.data.tabid}&level=${this.data.detail.customer_info.level}`,
    })
  },
  onChange(event) {
    // console.log(event)
    this.setData({
      activeNames: event.detail,
    });
  },
  onPageScroll(e) {
    this.data.scrolltop = e.scrollTop;
  },
  handleRollIn() {
    App._post_form('Resource/rollinToResource', {
      customer_id: this.data.id,
      content: ''
    }, function (result) {
      if (result.code === 0) {
        wx.showToast({
          title: '捞入成功',
          duration: 2000,
          success: () => {
            setTimeout(() => {
              wx.switchTab({
                url: '../social/index',
              })
            }, 2000);
          }
        })
      } else {
        wx.showToast({
          title: result.msg,
          icon: 'error'
        })
      }
    })
  },
  //拨打电话
  makePhoneCall(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.tel,
      success: function () {
        console.log('拨打成功')
      },
      fail: function () {
        console.log('拨打失败')
      }
    })
  },

  //我的、协防客户详情
  getCustomerDetail(c_id) {
    let _this = this
    App._post_form('customer/getCustomerInfo', {
      c_id
    }, function (result) {
      let record_list = result.data.record_list;
      let record_filed_name = result.data.record_filed_name;
      // console.log(record_filed_name);
      let new_list = [];
      for (var i = 0; i < record_list.length; i++) {
        if (record_list[i].operation == 'follow' || record_list[i].operation == 'inspect') {
          new_list.push(i);
        }
      }
      let str = result.data.customer_info.mobile.substr(3,1)
      // str = isNaN(str)
      console.log(str)
      _this.setData({
        detail: result.data,
        activeNames: new_list,
        is_tel:str == '*' ? false : true,
        record_filed_name: record_filed_name
      })
    });
  },
  //公池客户详情
  getPublicCustomerDetail(c_id) {
    let _this = this
    App._post_form('resource/getResourceLInfo', {
      c_id
    }, function (result) {
      wx.setStorage({
        key: 'consumer_id',
        data: c_id
      })
      let record_list = result.data.record_list;
      let new_list = [];
      for (var i = 0; i < record_list.length; i++) {
        if (record_list[i].operation == 'follow' || record_list[i].operation == 'inspect') {
          new_list.push(i);
        }
      }
      let str = result.data.customer_info.mobile.substr(3,1)
      console.log(result);
      _this.setData({
        detail: result.data,
        activeNames: new_list,
        is_tel:str == '*' ? false : true
      })
    });
  },
  copyName(e){
    let text = e.currentTarget.dataset.text
    wx.setClipboardData({
      //准备复制的数据内容
      data: text,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
        });
      }
    });
  },
  //获取真实手机号
  getRealPhone(e) {
    let text = e.currentTarget.dataset.text
    let _this = this
    if(_this.data.curIndex == 2 && _this.data.is_tel == false){
      wx.showModal({
        title: '是否查看手机号？',
        success: (res) => {
          if (res.confirm) {
            App._post_form('resource/showMobile', {
              customer_id: _this.data.id
            }, function (result) {
              if (result.code === 0) {
                wx.showToast({
                  title: '查看成功！',
                  success: () => {
                    _this.getPublicCustomerDetail(_this.data.id)
                  }
                })
              } else {
                Toast.fail(result.msg)
              }
            })
          }
        }
      })
    }else{
      console.log('222222')
      wx.setClipboardData({
        //准备复制的数据内容
        data: text,
        success: function (res) {
          wx.showToast({
            title: '复制成功',
          });
        }
      });
    }
  },


  //锁定或解锁客户
  lockCustomerSubmit(e) {
    let _this = this
    let { lock } = e.currentTarget.dataset
    console.log(lock)
    let text = lock == 1 ? '解锁' : '锁定'
    wx.showModal({
      title: `是否${text}?`,
      success: (res) => {
        if (res.confirm) {
          App._post_form('Customer/lockCustomer', {
            customer_id: _this.data.id
          }, function (result) {
            if (result.code === 0) {
              wx.showToast({
                title: `${text}成功`,
              })
              _this.getCustomerDetail(_this.data.id)
            }
            else {
              Toast.fail(result.msg)
            }
          })
        }
      }
    })
  },

  inputOnChange(e) {
    let input = e.currentTarget.dataset.input
    let value = e.detail
    this.setData({
      follow_form: {
        ...this.data.follow_form,
        [input]: value
      },
    })
  },
  //跟进记录提交
  customerFollow() {
    let _this = this
    App._post('customer/customerFollow', this.data.follow_form, function (result) {
      if (result.msg == '客户跟进提交成功') {
        Toast({
          type: 'success',
          message: '提交成功',
          onClose: () => {
            // wx.switchTab({
            //   url: '../index/index?val=2',
            // })
            // wx.navigateBack({//返回
            //   delta: 1
            // })
            _this.getPublicCustomerDetail(_this.data.id)
          },
        });
      } else {
        Toast.fail(result.msg);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})